import { getUserInfo, getUserPhoto, login } from '@/api/user'
import { setToken, removeToken, getToken } from '@/utils/auth'
import { Message } from 'element-ui'
export default {
  // 放入核心配置项
  namespaced: true, // 子模块开启命名空间
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    updataToken(state, payload) {
      state.token = payload
      setToken(payload)
    },
    delToken(state) {
      state.token = ''
      removeToken()
    },
    updateUserInfo(state, payload) {
      state.userInfo = payload
    }

  },
  actions: {
    async setUserInfo(context) {
      const res = await getUserInfo()
      const info = await getUserPhoto(res.data.userId)
      // console.log(res, info)
      context.commit('updateUserInfo', { ...res.data, ...info.data })
    },
    logout(context) {
      context.commit('updateUserInfo', {})
      context.commit('delToken')
    },
    async  goLogin(context, loginForm) {
      const res = await login(loginForm)
      // console.log(res)
      if (res.code !== 10000) return Message.error(res.message)
      Message.success(res.message)
      context.commit('updataToken', res.data)
    }
  }
}
